home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swagn_r.zip / PARSING.SWG / 0006_Command Line Parsing.pas < prev    next >
Pascal/Delphi Source File  |  1993-10-28  |  4KB  |  129 lines

  1. {===========================================================================
  2.  BBS: Canada Remote Systems
  3. From: RYAN THOMPSON
  4. Subj: RE: COMMAND LINE PARSING
  5.  
  6. >>> Quoting from Chet Kress to Frans Van Duinen about Command Line Parsing
  7.  
  8. CK>  FVD>I want to pass to my BP 7 program a few parameters, one of which
  9. CK>  FVD>has embedded (or even trailing) blanks.  The naive approach of
  10. CK>  FVD>PROCFAX  PROCFAX.CFG \PCB\MAIN\MSGS58  "FAX MAIL" does not work.
  11. CK>  FVD>Currently  I pick up FAX and MAIL as two parameters and
  12. CK>  FVD>string, but I want to allow multiple embedded/trailing blanks.
  13.  
  14.   Here's a set of routines to do just what you want.
  15.  
  16.   Parameters      Returns the number of parameters on the command line.  Does
  17.                    not include switches.
  18.   Parameter(n)    Returns the nth parameter, ignoring switches and passing
  19.                    strings in quotes as " or ' followed by the entire string
  20.                    including any imbedded spaces.
  21.   SwitchThere(x)  Returns True if the switch specified by the character
  22.                    passed is present on the command line.
  23.   SwitchData(x)   Returns the data following the switch character if the
  24.                    switch character specified is present on the command line.
  25.   SwitchNum(x)    Returns the position on the command line of the switch
  26.                    specified.  Skips parameters. }
  27.  
  28.  
  29.   Function SwitchNum(S : String) : Integer;
  30.     Var
  31.       Temp : String;
  32.       X,
  33.       Y : Integer;
  34.     Begin
  35.       Temp:= '';
  36.       X:= ParamCount;
  37.       Y:= 0;
  38.       while (X > 0) and (Y = 0) do begin
  39.         Temp:= ParamStr(X);
  40.         if (Temp[1] = '/') or (Temp[1] = '-') then
  41.           if UpCase(Temp[2]) = UpString(S) then Y:= X;
  42.         Dec(X);
  43.       end;
  44.       SwitchNum:= Y;
  45.     End;
  46.  
  47.  
  48.   Function SwitchThere(S : String) : Boolean;
  49.     Begin
  50.       SwitchThere:= not (SwitchNum(S) = 0);
  51.     End;
  52.  
  53.  
  54.   Function SwitchData(S : String) : String;
  55.     Var
  56.       Temp : String;
  57.     Begin
  58.       If SwitchNum(S) > 0 then begin
  59.         Temp:= ParamStr(SwitchNum(S));
  60.         Delete(Temp, 1, 2);
  61.       end
  62.       else Temp:= '';
  63.       SwitchData:= Temp;
  64.     End;
  65.  
  66.  
  67.   Function Parameter(N : Byte) : String;
  68.     Var
  69.       X,
  70.       Count : Byte;
  71.       Parm,
  72.       Temp : String;
  73.     Begin
  74.       X:= 0;
  75.       Count:= 0;
  76.       Parm:= '';
  77.       If ParamCount > 0 then repeat
  78.         Inc(X);
  79.         Temp:= ParamStr(X);
  80.         If (Temp[1] = '"') or (Temp[1] = '''') then begin
  81.           Parm:= Temp;
  82.           If X < ParamCount then repeat
  83.             Inc(X);
  84.             Parm:= Parm + ' ' + ParamStr(X);
  85.           until (Parm[Length(Parm)] = '"') or
  86.                 (Parm[Length(Parm)] = '''') or (X = ParamCount);
  87.           Inc(Count);
  88.         end
  89.         else if (Temp[1] <> '/') and (Temp[1] <> '-')
  90.         then begin
  91.           Inc(Count);
  92.           Parm:= Temp;
  93.         end;
  94.       until (X = ParamCount) or (Count = N);
  95.       If Count = N then Parameter:= Parm
  96.       else Parameter:= '';
  97.     End;
  98.  
  99.  
  100.   Function Parameters : Byte;
  101.     Var
  102.       X : Byte;
  103.     Begin
  104.       X:= 0;
  105.       If ParamCount > 0 then begin
  106.         Repeat
  107.           Inc(X)
  108.         Until Parameter(X) = '';
  109.         Parameters:= X - 1;
  110.       end
  111.       else Parameters:= 0;
  112.     End;
  113.  
  114. {
  115.   For example, the command line:
  116.  
  117. TESTPRG /C INPUT.DAT /X67 "first one"
  118.  
  119.         Parameters  returns  2
  120.       Parameter(1)  returns  INPUT.DAT
  121.       Parameter(2)  returns  "first one
  122.   SwitchThere('F')  returns  false
  123.    SwitchData('X')  returns  67
  124.  
  125.   Notice that in quoted parameters, the first quote is returned- this allows
  126. you to check for " vs. ', which you could use as the difference between case
  127. sensitive and non-case-sensitive.  A simple Delete(S,1,1) can remove it from
  128. the string for use. }
  129.